package com.test.steps;

import com.test.browser.BasePage;
import com.test.browser.Browser;
import com.test.pageObjects.IndexPage;
import com.test.pageObjects.LoginPage;
import io.cucumber.java.After;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;

public class LoginStep {
    WebDriver driver = null;
    LoginPage loginPage = null;

    @Given("打开登录网址")
    public void openLogin() {
        driver = new Browser().openBrowser();
        BasePage basePage = new BasePage(driver);
        basePage.open("http://mgr.saas.hkdev.bowenfin.com/alogin/");
        //driver.get("https://cloud.jxycrm.com/pc/#/login?redirect=%2Find");
    }

    @When("电话号输入 {string} 密码输入 {string} 并点击登录按钮")
    public void login(String string, String string2) {
        loginPage = new LoginPage(driver);
        loginPage.login(string,string2);
    }

    @Then("登录成功")
    public void loginSuccess() {
        IndexPage indexPage = new IndexPage(driver);
        boolean result = indexPage.usernameIsDisplayed();
        Assert.assertTrue(result);
    }

//    @Then("判断 {string} 登录情况是否正确 {string}")
//    public void loginResult(String state,String message){
//        if(state.equalsIgnoreCase("正确")){
//            IndexPage indexPage = new IndexPage(driver);
//            boolean result = indexPage.usernameIsDisplayed();
//            Assert.assertTrue(result);
//        } else if (state.equalsIgnoreCase("密码未输入")) {
//            Assert.assertEquals(loginPage.getPasswordError(),message);
//        } else if (state.equalsIgnoreCase("电话未输入")) {
//            Assert.assertEquals(loginPage.getPhoneError(),message);
//        } else {
//            Assert.assertEquals(loginPage.getError(),message);
//        }
//    }

    @Then("判断 {string} 登录情况是否正确 {string}")
    public void assertLoginResult(String state, String message) {
        if(state.equalsIgnoreCase("正确")){
            IndexPage indexPage = new IndexPage(driver);
            boolean result = indexPage.usernameIsDisplayed();
            Assert.assertTrue(result);
        } else if (state.equalsIgnoreCase("密码未输入")) {
            Assert.assertEquals(loginPage.getPasswordError(),message);
        } else if (state.equalsIgnoreCase("电话未输入")) {
            Assert.assertEquals(loginPage.getPhoneError(),message);
        } else {
            Assert.assertEquals(loginPage.getError(),message);
        }
    }

    @After()
    public void closeBrowser() {
        driver.quit();
    }
}
